﻿using AbpVnext_Module.Iservice;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Http.Client;
using Volo.Abp.Modularity;

namespace Ebusiness_Seckills
{
    [DependsOn(
           typeof(AbpAspNetCoreMvcModule),
              typeof(AbpHttpClientModule) //动态C#客户端
              ,typeof(AbpAutofacModule)
           )]
    public class SeckillsModule : AbpModule
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="context"></param>
        public override void ConfigureServices(ServiceConfigurationContext context)
        {

            IServiceCollection services = context.Services;
            IConfiguration configuration = services.GetConfiguration();
            //context.Services.GetHostingEnvironment().ContentRootPath
            // Add services to the container.
            //配置自定义控制器
            ConfigurControllers(context, configuration);
            //配置日志工厂模式
            ConfigLogFactory(context, configuration);
            //配置Swagger
            ConfigureSwaggerServices(context, configuration);
            //身份认证
            ConfigAuthentication(context);
            //配置动态 c#  客户端代理
            ConfigDynamicProxyClinet(context);
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            IApplicationBuilder app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            if (context.GetEnvironment().IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
            app.UseStaticFiles();
            //使用路由中间件
            app.UseRouting();
            app.UseAuthentication();
            //身份鉴权中间件
            app.UseAuthorization();
            //app.UseHttpsRedirection();
            app.UseConfiguredEndpoints(); //执行Abp Action
        }
        
        /// <summary>
        /// 配置动态代理客户端
        /// </summary>
        /// <param name="context"></param>
        public void ConfigDynamicProxyClinet(ServiceConfigurationContext context) 
        {
            //手动创建动态代理
            context.Services.AddHttpClientProxy<IProductImageService>("AbpVnext_Module");
            //context.Services.AddHttpClientProxy<IPermissionGrantsService>("AbpVnext_Module");
            //context.Services.AddHttpClientProxy<IPermissionService>("AbpVnext_Module");

            //手动批量创建动态代理
            //context.Services.AddHttpClientProxies(typeof(SeckillsModule).Assembly, "AbpVnext_Module");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ConfigAuthentication(ServiceConfigurationContext context)
        {
            context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                     .AddJwtBearer(options =>
                     {
                         options.Authority = "https://localhost:5272";  //认证中心校验 
                         options.Audience = "Ebusiness_Seckills";   //访问具体微服务对应的什么资源
                     });
            context.Services.AddAuthorization();
        }

        /// <summary>
        /// 配置自定义控制器
        /// </summary>
        public void ConfigurControllers(ServiceConfigurationContext context, IConfiguration configuration)
        {
            //配置自定义过滤器
            context.Services.AddControllers(option =>
            {
                //全局取消默认token校验 忽略xss攻击
                option.Filters.Add(new IgnoreAntiforgeryTokenAttribute());
            });
            //servicesout configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            context.Services.AddEndpointsApiExplorer();
        }

        /// <summary>
        /// 配置日志工厂模式
        /// </summary>
        public void ConfigLogFactory(ServiceConfigurationContext context, IConfiguration configuration)
        {
            ////配置日志工厂
            //context.Services.AddLogging(x =>
            //{
            //    x.AddConfiguration(configuration.GetSection("Logging"));
            //    x.AddLog4Net("log4Net.config");
            //    x.AddConsole();
            //});
        }

        /// <summary>
        /// 配置Swagger
        /// </summary>
        /// <param name="context"></param>
        /// <param name="configuration"></param>
        public void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
        {
            //不带身份验证注册
            context.Services.AddSwaggerGen();
        }
    }
}
